1. Project Clover database Tue Apr 11 2023 12:41:06 EDT
  2. Package org.joda.time.chrono

File ISOChronology.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

18
42
16
2
242
107
25
0.6
2.62
8
1.56

Classes

Class Line # Actions
ISOChronology 48 38 0% 21 3
0.955882495.6%
ISOChronology.Stub 219 4 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 2760 tests. .

Source view

1    /*
2    * Copyright 2001-2014 Stephen Colebourne
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16    package org.joda.time.chrono;
17   
18    import java.io.IOException;
19    import java.io.ObjectInputStream;
20    import java.io.ObjectOutputStream;
21    import java.io.Serializable;
22    import java.util.concurrent.ConcurrentHashMap;
23   
24    import org.joda.time.Chronology;
25    import org.joda.time.DateTimeFieldType;
26    import org.joda.time.DateTimeZone;
27    import org.joda.time.field.DividedDateTimeField;
28    import org.joda.time.field.RemainderDateTimeField;
29   
30    /**
31    * Implements a chronology that follows the rules of the ISO8601 standard,
32    * which is compatible with Gregorian for all modern dates.
33    * When ISO does not define a field, but it can be determined (such as AM/PM)
34    * it is included.
35    * <p>
36    * With the exception of century related fields, ISOChronology is exactly the
37    * same as {@link GregorianChronology}. In this chronology, centuries and year
38    * of century are zero based. For all years, the century is determined by
39    * dropping the last two digits of the year, ignoring sign. The year of century
40    * is the value of the last two year digits.
41    * <p>
42    * ISOChronology is thread-safe and immutable.
43    *
44    * @author Stephen Colebourne
45    * @author Brian S O'Neill
46    * @since 1.0
47    */
 
48    public final class ISOChronology extends AssembledChronology {
49   
50    /**
51    * Serialization lock
52    */
53    private static final long serialVersionUID = -6212696554273812441L;
54   
55    /**
56    * Singleton instance of a UTC ISOChronology
57    */
58    private static final ISOChronology INSTANCE_UTC;
59   
60    /**
61    * Cache of zone to chronology
62    */
63    private static final ConcurrentHashMap<DateTimeZone, ISOChronology> cCache = new ConcurrentHashMap<DateTimeZone, ISOChronology>();
64   
 
65  2 toggle static {
66  2 INSTANCE_UTC = new ISOChronology(GregorianChronology.getInstanceUTC());
67  2 cCache.put(DateTimeZone.UTC, INSTANCE_UTC);
68    }
69   
70    /**
71    * Gets an instance of the ISOChronology.
72    * The time zone of the returned instance is UTC.
73    *
74    * @return a singleton UTC instance of the chronology
75    */
 
76  3534429 toggle public static ISOChronology getInstanceUTC() {
77  3534429 return INSTANCE_UTC;
78    }
79   
80    /**
81    * Gets an instance of the ISOChronology in the default time zone.
82    *
83    * @return a chronology in the default time zone
84    */
 
85  14878 toggle public static ISOChronology getInstance() {
86  14878 return getInstance(DateTimeZone.getDefault());
87    }
88   
89    /**
90    * Gets an instance of the ISOChronology in the given time zone.
91    *
92    * @param zone the time zone to get the chronology in, null is default
93    * @return a chronology in the specified time zone
94    */
 
95  17112 toggle public static ISOChronology getInstance(DateTimeZone zone) {
96  17112 if (zone == null) {
97  41 zone = DateTimeZone.getDefault();
98    }
99  17112 ISOChronology chrono = cCache.get(zone);
100  17112 if (chrono == null) {
101  79 chrono = new ISOChronology(ZonedChronology.getInstance(INSTANCE_UTC, zone));
102  79 ISOChronology oldChrono = cCache.putIfAbsent(zone, chrono);
103  79 if (oldChrono != null) {
104  0 chrono = oldChrono;
105    }
106    }
107  17112 return chrono;
108    }
109   
110    // Constructors and instance variables
111    //-----------------------------------------------------------------------
112   
113    /**
114    * Restricted constructor
115    */
 
116  81 toggle private ISOChronology(Chronology base) {
117  81 super(base, null);
118    }
119   
120    // Conversion
121    //-----------------------------------------------------------------------
122   
123    /**
124    * Gets the Chronology in the UTC time zone.
125    *
126    * @return the chronology in UTC
127    */
 
128  28720 toggle public Chronology withUTC() {
129  28720 return INSTANCE_UTC;
130    }
131   
132    /**
133    * Gets the Chronology in a specific time zone.
134    *
135    * @param zone the zone to get the chronology in, null is default
136    * @return the chronology
137    */
 
138  1119 toggle public Chronology withZone(DateTimeZone zone) {
139  1119 if (zone == null) {
140  17 zone = DateTimeZone.getDefault();
141    }
142  1119 if (zone == getZone()) {
143  536 return this;
144    }
145  583 return getInstance(zone);
146    }
147   
148    // Output
149    //-----------------------------------------------------------------------
150   
151    /**
152    * Gets a debugging toString.
153    *
154    * @return a debugging string
155    */
 
156  9 toggle public String toString() {
157  9 String str = "ISOChronology";
158  9 DateTimeZone zone = getZone();
159  9 if (zone != null) {
160  9 str = str + '[' + zone.getID() + ']';
161    }
162  9 return str;
163    }
164   
 
165  81 toggle protected void assemble(Fields fields) {
166  81 if (getBase().getZone() == DateTimeZone.UTC) {
167    // Use zero based century and year of century.
168  2 fields.centuryOfEra = new DividedDateTimeField(
169    ISOYearOfEraDateTimeField.INSTANCE, DateTimeFieldType.centuryOfEra(), 100);
170  2 fields.centuries = fields.centuryOfEra.getDurationField();
171   
172  2 fields.yearOfCentury = new RemainderDateTimeField(
173    (DividedDateTimeField) fields.centuryOfEra, DateTimeFieldType.yearOfCentury());
174  2 fields.weekyearOfCentury = new RemainderDateTimeField(
175    (DividedDateTimeField) fields.centuryOfEra, fields.weekyears, DateTimeFieldType.weekyearOfCentury());
176    }
177    }
178   
179    //-----------------------------------------------------------------------
180   
181    /**
182    * Checks if this chronology instance equals another.
183    *
184    * @param obj the object to compare to
185    * @return true if equal
186    * @since 1.6
187    */
 
188  1050 toggle public boolean equals(Object obj) {
189  1050 if (this == obj) {
190  990 return true;
191    }
192  60 if (obj instanceof ISOChronology) {
193  9 ISOChronology chrono = (ISOChronology) obj;
194  9 return getZone().equals(chrono.getZone());
195    }
196  51 return false;
197    }
198   
199    /**
200    * A suitable hash code for the chronology.
201    *
202    * @return the hash code
203    * @since 1.6
204    */
 
205  30129 toggle public int hashCode() {
206  30129 return "ISO".hashCode() * 11 + getZone().hashCode();
207    }
208   
209    //-----------------------------------------------------------------------
210   
211    /**
212    * Serialize ISOChronology instances using a small stub. This reduces the
213    * serialized size, and deserialized instances come from the cache.
214    */
 
215  16 toggle private Object writeReplace() {
216  16 return new Stub(getZone());
217    }
218   
 
219    private static final class Stub implements Serializable {
220    private static final long serialVersionUID = -6212696554273812441L;
221   
222    private transient DateTimeZone iZone;
223   
 
224  16 toggle Stub(DateTimeZone zone) {
225  16 iZone = zone;
226    }
227   
 
228  28 toggle private Object readResolve() {
229  28 return ISOChronology.getInstance(iZone);
230    }
231   
 
232  16 toggle private void writeObject(ObjectOutputStream out) throws IOException {
233  16 out.writeObject(iZone);
234    }
235   
 
236  28 toggle private void readObject(ObjectInputStream in)
237    throws IOException, ClassNotFoundException {
238  28 iZone = (DateTimeZone) in.readObject();
239    }
240    }
241   
242    }